package com.market.fleamarket.controller;

import com.market.fleamarket.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class ViewController {
    private static final Logger logger = LoggerFactory.getLogger(ViewController.class);

    /**
     * 首页
     */
    @GetMapping("/")
    public String index() {
        return "index";
    }

    /**
     * 登录页面
     */
    @GetMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 注册页面
     */
    @GetMapping("/register")
    public String register() {
        return "register";
    }

    /**
     * 商品列表页面
     */
    @GetMapping("/products")
    public String products() {
        return "products";
    }

    /**
     * 商品详情页面
     */
    @GetMapping("/product-detail")
    public String productDetail(@RequestParam("id") Long id, Model model) {
        logger.info("访问商品详情页面，商品ID: {}", id);
        model.addAttribute("productId", id);
        return "product-detail";
    }

    /**
     * 购物车页面
     */
    @GetMapping("/cart")
    public String cart(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "cart";
    }

    /**
     * 购买订单页面
     */
    @GetMapping("/orders")
    public String orders(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "orders";
    }

    /**
     * 管理员页面
     */
    @GetMapping("/admin")
    public String admin(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || (!("admin".equals(user.getRole()) || "category_admin".equals(user.getRole())))) {
            return "redirect:/login";
        }
        return "admin";
    }

    /**
     * 我的评价页面
     */
    @GetMapping("/my-reviews")
    public String myReviews(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "my-reviews";
    }

    /**
     * 个人资料页面
     */
    @GetMapping("/profile")
    public String profile(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "profile";
    }

    /**
     * 修改密码页面
     */
    @GetMapping("/change-password")
    public String changePassword(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "change-password";
    }

    /**
     * 我的商品页面
     */
    @GetMapping("/my-products")
    public String myProducts(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "my-products";
    }

    /**
     * 发布商品页面
     */
    @GetMapping("/publish-product")
    public String publishProduct(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "publish-product";
    }

    /**
     * 编辑商品页面
     */
    @GetMapping("/edit-product")
    public String editProduct(@RequestParam Long id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "edit-product";
    }

    /**
     * 卖家订单页面
     */
    @GetMapping("/seller-orders")
    public String sellerOrders(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "seller-orders";
    }
}